﻿using System.Text;
using System.Collections.Specialized;
using Newtonsoft.Json;

namespace CometDocs
{
    internal partial class Client
    {
        /// <summary>
        /// Get the content of a folder: https://www.cometdocs.com/developer/apiDocumentation#method-getFolder
        /// </summary>
        /// <param name="token">A valid authentication token.</param>
        /// <param name="folder">A folder or null for the root folder.</param>
        /// <param name="recursive">True if sub-folders content should be retrieved too.</param>
        /// <returns>A Folder instance.</returns>
        public Folder GetFolder(AuthenticationToken token, FolderInfo folder = null, bool? recursive = null)
        {
            NameValueCollection @params = new NameValueCollection(3);
            @params.Add("token", token.Value);
            if (folder != null)
            {
                @params.Add("folderId", folder.ID.ToString());
            }
            if (recursive != null)
            {
                @params.Add("recursive", recursive.Value ? "1" : "0");
            }

            byte[] result = webClient.UploadValues(APIRoot + "getFolder", @params);

            string json = Encoding.ASCII.GetString(result);

            GetFolderResponse response = JsonConvert.DeserializeObject<GetFolderResponse>(json);

            CheckAndThrow(response);

            return response.Folder;
        }
    }
}
